package controllers

import command.CommandExec
import dao.ModeDao
import play.api.libs.json.Json
import play.api.mvc.{AbstractController, ControllerComponents}
import tool.Pojo.CommandData
import tool.{FormTool, Tool}
import utils.Utils

import java.io.File
import javax.inject.Inject
import implicits.Implicits._

/**
 * Created by yz on 7/5/2021
 */
class ToolController @Inject()(cc:ControllerComponents)(
                              implicit val modeDao: ModeDao
) extends AbstractController(cc){

  def image2jpeg = Action {
    implicit request =>
      val startTime = System.currentTimeMillis()
      val data = FormTool.base64Form.bindFromRequest().get
      val tmpDir = Tool.createTempDirectory("tmpDir")
      val inputFile = new File(tmpDir, "input.png")
      data.base64.base642pngFile(inputFile)
      val outFile = new File(tmpDir, "out.jpg")
      val command =
        s"""
           |convert ${inputFile.unixPath}  ${outFile.unixPath}
           |""".stripMargin
      val commandExec = CommandExec().exec { b =>
        CommandData(tmpDir, command)
      }

      val json=if (commandExec.isSuccess) {
       Json.obj("valid" -> true, "base64" -> outFile.base64)
      } else {
        Json.obj("valid" -> false, "message" -> commandExec.errorInfo)
      }
      Tool.deleteDirectory(tmpDir)
      Ok(json)
  }

  def png2pdf = Action {
    implicit request =>
      val startTime = System.currentTimeMillis()
      val data = FormTool.base64Form.bindFromRequest().get
      val tmpDir = Tool.createTempDirectory("tmpDir")
      val inputFile = new File(tmpDir, "input.png")
      val outFile = new File(tmpDir, "out.pdf")
      data.base64.base642pngFile(inputFile)
      val command =
        s"""
           |convert ${inputFile.unixPath}  ${outFile.unixPath}
           |""".stripMargin
      val commandExec = CommandExec().exec { b =>
        CommandData(tmpDir, command)
      }

      val json=if (commandExec.isSuccess) {
       Json.obj("valid" -> true, "base64" -> outFile.base64)
      } else {
        Json.obj("valid" -> false, "message" -> commandExec.errorInfo)
      }
      Tool.deleteDirectory(tmpDir)
      Ok(json)
  }

  def image2svg = Action {
    implicit request =>
      val startTime = System.currentTimeMillis()
      val data = FormTool.base64Form.bindFromRequest().get
      val tmpDir = Tool.createTempDirectory("tmpDir")
      val imageType = FormTool.imageTypeForm.bindFromRequest().get.imageType
      val inputFile = imageType match {
        case "png" => val inputFile = new File(tmpDir, "input.png")
          data.base64.base642pngFile(inputFile)
        case "pdf" => val inputFile = new File(tmpDir, "input.pdf")
          data.base64.base642pdfFile(inputFile)
      }
      val outFile = new File(tmpDir, "out.svg")
      val command = imageType match {
        case "pdf" =>
          s"""
             |inkscape -l ${outFile.unixPath} ${inputFile.unixPath}
             |""".stripMargin
        case "png" =>
          s"""
             |convert ${inputFile.unixPath}  ${outFile.unixPath}
             |""".stripMargin
      }

      val commandExec = CommandExec().exec { b =>
        CommandData(tmpDir, command)
      }

      val json=if (commandExec.isSuccess) {
      Json.obj("valid" -> true, "base64" -> outFile.base64)
      } else {
        Json.obj("valid" -> false, "message" -> commandExec.errorInfo)
      }
      Tool.deleteDirectory(tmpDir)
      Ok(json)
  }

  def getFileData = Action { implicit request =>
    val data = FormTool.fileNameForm.bindFromRequest().get
    val file = new File(Tool.dataDir, data.fileName)
    val (columnNames, array) = Utils.getInfoByLines(file.txtLines)
    val json = Json.obj("columnNames" -> columnNames, "array" -> array)
    Ok(json)
  }

  def getNoHeaderFileData = Action { implicit request =>
    val data = FormTool.fileNameForm.bindFromRequest().get
    val file = new File(Tool.dataDir, data.fileName)
    val array = file.txtLines.map { tmpColumns =>
      val columns=tmpColumns.map(_.trimQuote)
      (columns(0),columns(1))
    }.toSeqMap
    val json = Json.obj("array" -> array)
    Ok(json)
  }



}
